unit clsFotoProduto;

interface

uses dbtables, db, extctrls, graphics, classes, jpeg, dm, SysUtils;

type
  TFotoProduto = class
  public
    Caminho: string;
    procedure Gravar(pcod_prod: string; var pjpeg: TJpegImage);
    procedure Excluir(pcod_prod: string);
    function Recuperar(pcod_prod: string; var pjpeg: TJPEGImage): boolean; overload;
    function Recuperar(pcod_prod: string; var pbitmap: TBitmap): boolean; overload;
  end;

implementation

procedure TFotoProduto.Gravar;
var
  tq: TQuery;
  ms: TMemoryStream;

begin
  ms := TMemoryStream.Create;
  pJpeg.savetostream(ms);
  ms.position := 0;
  tq := TQuery.Create(dmint);
  tq.DatabaseName := dmint.db.DatabaseName;
  tq.active := false;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Insert into FotoProduto values (:KCOD_PROD, :KFOTO, :KCAMINHO)');
  tq.ParambyName('KCOD_PROD').asstring := pcod_prod;
  tq.ParambyName('KFOTO').LoadFromStream(ms, ftGraphic);
  tq.ParamByName('KCAMINHO').AsString := Caminho;
  tq.ExecSQL;
  ms.Free;
  tq.close;
  tq.Free;

end;

function TFotoProduto.Recuperar(pcod_prod: string; var pjpeg: TJPEGImage): boolean;
var
  tq: TQuery;
  ms: TMemoryStream;

begin
  tq := TQuery.Create(dmint);
  tq.DatabaseName := dmint.db.DatabaseName;
  tq.active := false;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select * from FotoProduto where Pro_Codigo=:a');
  tq.params[0].asstring := pcod_prod;
  tq.Open;
  if not tq.eof then begin
    if not (tq.fieldbyname('Foto').isNull) then begin
      ms := TMemoryStream.Create;
      TBlobField(tq.Fieldbyname('Foto')).SaveToStream(ms);
      ms.position := 0;
      try
        pJpeg.LoadFromStream(ms);
      except
        pJpeg := nil;
      end;

      ms.free;

    end else begin
      //if Caminho <> '' then

    end;
    Caminho := tq.FieldByName('Caminho').AsString;
    result := true;

  end else begin
    result := false;
    Caminho := '';
  end;

  tq.close;
  tq.free;
end;

function TFotoProduto.Recuperar(pcod_prod: string; var pbitmap: TBitmap): boolean;
var
  tq: TQuery;
  ms: TMemoryStream;

begin
  tq := TQuery.Create(dmint);
  tq.DatabaseName := dmint.db.DatabaseName;
  tq.active := false;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select * from FotoProduto where Pro_Codigo=:a');
  tq.params[0].asstring := pcod_prod;
  tq.Open;
  if not tq.eof then begin
    if not (tq.fieldbyname('Foto').isNull) then begin
      ms := TMemoryStream.Create;
      TBlobField(tq.Fieldbyname('Foto')).SaveToStream(ms);
      ms.position := 0;
      try
        pBitmap.LoadFromStream(ms);
      except
        pBitmap := nil;
      end;

      ms.free;

    end;
    Caminho := tq.FieldByName('Caminho').AsString;
    result := true;

  end else begin
    result := false;
    Caminho := '';
  end;

  tq.close;
  tq.free;

end;

procedure TFotoProduto.Excluir;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.DatabaseName := dmint.db.DatabaseName;
  tq.active := false;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Delete from FotoProduto where Pro_Codigo=:a');
  tq.params[0].asstring := pcod_prod;
  tq.ExecSQL;
  tq.Close;
  tq.Free;
end;

end.

